package example.strategy.json;

import org.apache.log4j.Logger;
import com.scop.desensitize.cs.entity.User;
import com.scop.desensitize.exception.strategy.DecodeException;
import com.scop.desensitize.exception.strategy.EncodeException;
import com.scop.desensitize.strategy.json.JsonListStrategy;
import com.scop.desensitize.strategy.rreversible.symmetry.DesStrategy;

import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @desc 实体对象转Json数据脱敏示例
 * @author gzy
 * @time 2022-05-01
 */
public class JsonListStrategyDemo {

    public static Logger log = Logger.getLogger(JsonListStrategyDemo.class);
    public static void main(String[] args) throws EncodeException, DecodeException {
        //套实体类
        User user = new User();
        user.setName("小明");

        List<Object> list = new ArrayList<Object>();
        //子 map
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> map2 = new HashMap<>();
        map2.put("key", "value");

        map.put("mapKEY", "mapVALUE");
        map.put("map中的map", map2);


        //子list
        List<Object> list2 = new ArrayList<Object>();
        list2.add(1);
        list2.add(map2);
        //添加基本数据类型
        list.add(1);
        //添加map
        list.add(map);
        //添加list
        list.add(list2);
        list.add(user);

        listToJson(list);
    }

    private static void listToJson(List list) throws EncodeException, DecodeException {
        JsonListStrategy jsonListStrategy = new JsonListStrategy(DesStrategy.getInstance());
        log.debug("原数据List：" + list);
        String result = jsonListStrategy.encode(list);
        log.debug("加密后：" + result);

        jsonToList(result, jsonListStrategy);
    }

    /**
     * @desc 解密
     * @param result
     * @param jsonListStrategy
     * @throws EncodeException
     * @throws NoSuchAlgorithmException
     */
    private static void jsonToList(String result, JsonListStrategy jsonListStrategy) throws DecodeException {
        //套map
        List resultList = jsonListStrategy.decode(result);
        log.debug("解密后：" + resultList);
    }
}
